-- Global variables support
RAID_CLASS_COLORS = {
    ["WARRIOR"] = { r = 0.78, g = 0.61, b = 0.43 },
    ["MAGE"] = { r = 0.41, g = 0.8, b = 0.94 },
    ["ROGUE"] = { r = 1.0, g = 0.96, b = 0.41 },
    ["CLERIC"] = { r = 1.0, g = 1.0, b = 1.0 },
    ["HUNTER"] = { r = 0.67, g = 0.83, b = 0.45 },
    ["WARLOCK"] = { r = 0.58, g = 0.51, b = 0.79 },
    ["PRIEST"] = { r = 1.0, g = 1.0, b = 1.0 },
    ["PALADIN"] = { r = 0.96, g = 0.55, b = 0.73 },
    ["DRUID"] = { r = 1.0, g = 0.49, b = 0.04 },
    ["SHAMAN"] = { r = 0.0, g = 0.44, b = 0.87 },
    ["DEATHKNIGHT"] = { r = 0.77, g = 0.12 , b = 0.23 },
}

EVENT_MAP_WOW = {
  	["ADDON_LOADED"] = Event.Addon.Load.End,
	["PLAYER_ENTERING_WORLD"] = Event.Unit.Available,
	["SPELLS_CHANGED"] = Event.Ability.Add,
	["UNIT_HEALTH"] = Event.Unit.Detail.Health,
	["UNIT_SPELLCAST_SENT"] = Event.Ability.Usable.False,
	["UNIT_SPELLCAST_INTERRUPTED"] = Event.Ability.Usable.True,
	["UNIT_SPELLCAST_SUCCEEDED"] = Event.Ability.Usable.True,
	["PLAYER_TALENT_UPDATE"] = Event.Ability.Add,
	["SPELL_UPDATE_COOLDOWN"] = Event.Ability.Cooldown.Begin,
	["PLAYER_REGEN_ENABLED"] = Event.Unit.Available,
	["UNIT_NAME_UPDATE"] = Event.Ability.Target,
	["UNIT_AURA"] = Event.Ability.Target,
}

WOWCompat_OnEvent = nil

local function getAbility(id)
  local list = Inspect.Ability.List()
  if not list then
    return
  end

  local details = Inspect.Ability.Detail(list)
  for abilityId, ability in pairs(details) do
   if abilityId == id then
    return ability
   end
  end
end

local function getDetail(unit)
  local detail = Inspect.Unit.Detail(unit)
  if detail then
    return detail
  end
end

local function handleEvent(frame, name, data)
  if WOWCompat_OnEvent ~= nil then
    for k, v in pairs(EVENT_MAP_WOW) do
      if k == name then
        Healium_OnEvent(frame, name, data)
      end
    end
  end
end

-- Global function support
DEFAULT_CHAT_FRAME = {}

function DEFAULT_CHAT_FRAME:AddMessage(msg)
  print(msg)
end

RegisterEvent = function(frame, name)
  local handle = function(data) handleEvent(frame, name, data) end
  if WOWCompat_OnEvent ~= nil then
    for k, v in pairs(EVENT_MAP_WOW) do
      if k == name then
        table.insert(v, { handle, "WOWCompat", k})
      end
    end
  end
end

UnitClass = function(unit)
  local detail = getDetail(unit)
  if detail then
    return detail.calling, detail.calling
  end
end

UnitHealth = function(unit)
  local detail = getDetail(unit)
  if detail then
    return detail.health
  end
end

UnitHealthMax = function(unit)
  local detail = getDetail(unit)
  if detail then
    return detail.healthMax
  end

end

UnitRace = function(unit)
  local detail = getDetail(unit)
  if detail then
    return detail.calling, detail.calling
  end
end

GetSpellInfo = function(id)
  local ability = getAbility(id)
  if ability then
    return ability
  end
end

GetTime = function()
  return Inspect.Time.Real()
end

GetActiveTalentGroup = function()
  return 1
end

InCombatLockdown = function()
 return false
end